package ltd.dujiabao.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.springcloud.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.text.html.HTMLDocument;
import java.util.concurrent.TimeUnit;

/**
 * @author : DuJiabao
 * @Project : springcloud
 * @Time : 2021/5/24 21:04
 * @Desc :
 */
@Api("流量控制")
@RestController
@Slf4j
public class FlowLimitController {
  @Autowired
  ResourceService resourceService;

  @GetMapping("/testA")
  public String testA() {
    resourceService.resource();
    return "------testA";
  }

  @GetMapping("/testB")
  public String testB() {
    resourceService.resource();
    return "------testB";
  }

  @GetMapping("/testRT")
  public String testRT() {
    try {
      TimeUnit.MILLISECONDS.sleep(300);  // 睡眠300ms
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return "睡了300ms，已结束";
  }

  @GetMapping("/testException")
  public String testException() {
    int result = 10 / 0;
    return "测试异常";
  }
  @SentinelResource(value = "testHotKey", blockHandler = "hotKeyHandler")
  @GetMapping("/testHotKey")
  public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                           @RequestParam(value = "p2", required = false) String p2) {
    return "------testHotKey";
  }

  public String hotKeyHandler(String p1, String p2, BlockException exception) {
    return "-----hotKeyHandler";
  }
}
